//
//  PayListViewController.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/3/16.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "PayListViewController.h"
#import "CheckoutModel.h"
#import "PaymethodTableViewCell.h"
@interface PayListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *payListTableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) PaymentMethodModel *payModel;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation PayListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.payListTableView.delegate = self;
    self.payListTableView.dataSource = self;
    self.confirmBtn.layer.cornerRadius = 24.f;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44.f)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 14, 80, 16)];
        label.font = [UIFont systemFontOfSize:16.f];
        label.text = @"支付方式";
        [_headView addSubview:label];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width - 44.f, 0.f, 44, 44)];
        [button setImage:[UIImage imageNamed:@"btn_shouqi"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closePageAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:button];
        
        UIView *spaceLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43.f, self.view.width, .8f)];
        spaceLine.backgroundColor = [UIColor colorWithRGB:0xe1e1e1];
        [_headView addSubview:spaceLine];
    }
    return _headView;
}


- (void)setPaymentListArray:(NSMutableArray *)paymentListArray {
    _paymentListArray = paymentListArray;
    [self.payListTableView reloadData];
}

- (void)closePageAction:(id)sender {
    if (self.closePayPageBlock) {
        self.closePayPageBlock();
    }
}

- (UIView *)footView {
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44.f)];
    }
    return _footView;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.paymentListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"PayListCell";
    PaymethodTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                        identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PaymethodTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectPayMethodBlock = ^(PaymentMethodModel *model) {
        self.payModel = model;
        if (model.selected) {
            for (PaymentMethodModel *tPaymodel in self.paymentListArray) {
                if (model != tPaymodel) {
                    tPaymodel.selected = NO;
                }
            }
        }
    };
    PaymentMethodModel *model = [self.paymentListArray objectAtIndex:indexPath.row];
    [cell updatePaymentInfo:model];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.footView;
}



- (IBAction)confirmAction:(id)sender {
    if (!self.payModel) {
        [[LKDHud shareHud] showtip:@"请选择支付方式"];
        return;
    }
    [ApiClient setPayment:self.payModel.code didFinishLoaded:^(KXJson *json, NSString *path) {
        if ([[json getStringForKey:@"status"] isEqualToString:@"1"]) {
            if (self.selectPayMethodBlock) {
                self.selectPayMethodBlock(self.payModel);
            }
            if (self.closePayPageBlock) {
                self.closePayPageBlock();
            }
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
